<?php
/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Game
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */
class Game_Widget_ProfileScoresController extends Engine_Content_Widget_Abstract
{
  public function indexAction()
  {
    // Don't render this if not authorized
    $viewer = Engine_Api::_()->user()->getViewer();
    if( !Engine_Api::_()->core()->hasSubject() ) {
      return $this->setNoRender();
    }

    // Get subject and check auth
    $this->view->game = $subject = Engine_Api::_()->core()->getSubject('game');
    
    if( !($subject instanceof Game_Model_Game) ) {
      return $this->setNoRender();
    }    
    if( !$subject->authorization()->isAllowed($viewer, 'view') ) {
      return $this->setNoRender();
    }
    if (!$subject->leaderboard_enabled) {
      return $this->setNoRender();
    }
    
    $params = array(
      'limit' => $this->_getParam('max', 5),
      'user' => $this->_getParam('user'),
      'order' => $this->_getParam('order', 'score'),
    );
    
    $this->view->scores = $scores = $subject->scores()->getGameHighScores($params);
    
    if (count($scores) == 0 && !$this->_getParam('showemptyresult', false)) {
      return $this->setNoRender();
    }    
    
    // speed thing up :-)
    Engine_Api::_()->getItemTable('game_score')->preloadUsers($scores);
  }
}